var rlyname='80002700000147';
var rlypassword='U9TRtDzO';
var serverVoipName = "4001000027"
/**
 * 视屏通话JS--自助端
 */
//初始化容联云
function ronglianyunInit(){
    RegisterCallBack(ECSDK, "onConnectState", onConnectState);
    RegisterCallBack(ECSDK, "onReceiveMessage", onReceiveMessage);
    RegisterCallBack(ECSDK, "onRequestShareDesktop", onRequestShareDesktop);
    RegisterCallBack(ECSDK, "onRequestRemoteControl", onRequestRemoteControl);
    RegisterCallBack(ECSDK, "onCallEvents", onCallEvents);
    Initialize();
    Login();
}

// 初始化
function Initialize() {
    ECSDK.Initialize();
    ECSDK.SetVideoView(remoteView.HWND, localView.HWND);
    ECSDK.SetCodecEnabled(7, false);
}

// 登录
function Login() {
    var info = new Object();
    info.authType = 3;
    
    info.username = rlyname;
    info.userPassword = rlypassword;
    
    info.appKey = "2c9082bd5127d79001512825c4090001";
    ECSDK.Login(info);
    if(isHt=='true'){
    	 GetCameraInfo();
    }
   }
//针对瀚泰设备调用高拍仪摄像头问题  
function GetCameraInfo() {
    var info = new Array();
    var result = ECSDK.GetCameraInfo(info);
    ECSDK.selectCamera(1, 5 ,33,0,false);
}

function MakeCall() {
	
	ECSDK.SetVideoView(remoteView.HWND, localView.HWND);
    var callid = new Object();
    //var serverVoipName = $("#voipNum").val();
    var result = ECSDK.MakeCall(callid, "1", serverVoipName);
    if(result != "0"){
        connectinfo("呼叫失败！");
    }
}


function RegisterCallBack(obj, name, proc) {
    if (typeof (proc) != "function")
        return;
    if (window.ActiveXObject || "ActiveXObject" in window) {
        if (window.ActiveXObject && obj.attachEvent) {
            obj.attachEvent(name, proc);
        } else {
            AttachIE11Event(obj, name, proc);
        }
    } else {
        obj[name] = proc;
    }
}

function AttachIE11Event(obj, _strEventId, _functionCallback) {
    var nameFromToStringRegex = /^function\s?([^\s(]*)/;
    var paramsFromToStringRegex = /\(\)|\(.+\)/;
    var params = _functionCallback.toString().match(paramsFromToStringRegex)[0];
    var functionName = _functionCallback.name || _functionCallback.toString().match(nameFromToStringRegex)[1];
    var handler;
    try {
        handler = document.createElement("script");
        handler.setAttribute("for", obj.id);
    } catch (ex) {
        handler = document.createElement('<script for="' + obj.id + '">');
    }
    handler.event = _strEventId + params;
    handler.appendChild(document.createTextNode(functionName + params + ";"));
    document.body.appendChild(handler);
}

// 以下为回调事件
// 登录成功回调
function onConnectState(msg)
{
	//0：连接成功，1：连接中，2：连接失败，3：同设备登录被踢，4：/强制下线
	if(msg.code == "0"){
		MakeCall();
	}else if(msg.code != "1"){
		connectinfo("登录失败！");
	}
}

var isConnected = false;


// 视频连接成功回调
function onReceiveMessage(msg)
{
	var content = msg.msgContent;
	// 应答成功
	if(content == 'agentanswer'){
		isConnected = true;
		my_vue.setFaceState(1);
	}else{
		connectinfo("视频连接失败！");
	}
}

// 请求桌面共享回调
function onRequestShareDesktop(msg)
{
	var result = ECSDK.AcceptShareDesktop();
}

//请求控制回调
function onRequestRemoteControl(msg)
{
	var result = ECSDK.AcceptRemoteControl();
}

// 通话状态改变回调
function onCallEvents(reason, call) {
    var eCode = call.eCode;
    // 挂断
    if(eCode == '4'){
    	// 连上之后挂断的情况
    	if(isConnected){
    		my_vue.exitCallBack();
    	}else{
    		// 面对面的时候，座席端未登录的情况
    		MakeCall();
    	}
    }
}